package com.example.boot.annotation;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JacksonConfig {
    @Resource
    private ObjectMapper objectMapper; // Spring Boot 的默认 ObjectMapper

    @Resource
    private DesensitizeProperties desensitizeProperties; // 注入脱敏配置类

    /**
     * 初始化方法：在 Bean 创建后执行，将脱敏配置绑定到 ObjectMapper 的上下文属性
     */
    @PostConstruct
    public void init() {
        // 关键步骤：将脱敏配置存入 ObjectMapper 的上下文属性
        objectMapper.setConfig(objectMapper.getSerializationConfig()
                .withAttribute(DesensitizeProperties.class, desensitizeProperties));
    }
}